package com.example.lanqiaodemo1.LanQiao;

import java.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Scanner;

public class Student1 {

	public static void main(String[] args)   {
		int studentCount=0;
		Scanner scanner=new Scanner(System.in);
		System.out.println("请输入要录入的学生的个数");
		studentCount=scanner.nextInt();
		//获取用户信息
		try {
			getUserInfo(studentCount);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		//输出用户信息
		try {
			showStudentInfo();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public static void  getUserInfo(int studentUserInfo) throws FileNotFoundException, IOException {
		List<Student> lists=new ArrayList<>();
		ObjectOutputStream objectOutputStream =
			    //填写你要输出到的位置
			    new ObjectOutputStream(new FileOutputStream("D:\\student.txt"));
		Scanner scanner=new Scanner(System.in);
		for(int i=0;i<studentUserInfo;i++) {
			Student student=new Student();
			System.out.println("请输入学生学号");
			int xh=scanner.nextInt();
			student.setXh(xh);
			scanner.nextLine();
			System.out.println("请输入学生的姓名");
			String name=scanner.nextLine();
			System.out.println(name);
			student.setName(name);
			lists.add(student);
		}
		//保存到我们的文件中去
		objectOutputStream.writeObject(lists);
		
	}
	
	public static void showStudentInfo() throws FileNotFoundException, IOException, Exception {
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("D:\\student.txt")));
		ArrayList<Student> Students = (ArrayList<Student>)in.readObject();
		for(Student student:Students) {
			System.out.println(student);
		}
	}

}
//实现串行化
class Student implements Serializable{
	private int xh;
	private String name;

	public Student () {
		
	}
	public Student(int xh, String name) {
		super();
		this.xh = xh;
		this.name = name;
	}
	public int getXh() {
		return xh;
	}
	public void setXh(int xh) {
		this.xh = xh;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "Student [xh=" + xh + ", name=" + name + "]";
	}
	
	
}